﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Drawing;
using DockSample.Line;
using DockSample.DAO;

namespace DockSample
{
    class OriginalModelFormDAO : BaseDAO
    {

        RFormToDiagramDAO rdbo = new RFormToDiagramDAO();
        OriginalLineDAO lineDBO = new OriginalLineDAO();
        public bool DeleteForm(DummyModelForm form)
        {
            try
            {
                this.conn = this.GetConnection();
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "delete from OriginalModelForm where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", form.id));
              
                cmd.ExecuteNonQuery();
                rdbo.DeleteRelations(form);

                //删除直线
                if (form.straightlineList != null)
                {
                    foreach (OriginalLine line in form.straightlineList)
                    {
                        lineDBO.DeleteLine(line);
                    }
                } 

                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                this.CloseConnection();
            }
        }


        public bool AddForm(DummyModelForm form)
        {
            try
            {
     //           this.DeleteForm(form);
                   this.conn = this.GetConnection();
               SQLiteCommand cmd = this.GetCmd();
             cmd.CommandText = "insert into OriginalModelForm( id ,x ,y,width,height,text,formtype,visible)  "
                   + "values (@id,@x,@y,@width,@height,@text, @formtype,@visible)";
                cmd.Parameters.Add(new SQLiteParameter("id", form.id));
                cmd.Parameters.Add(new SQLiteParameter("x", form.Location.X));
                cmd.Parameters.Add(new SQLiteParameter("y", form.Location.Y));
                cmd.Parameters.Add(new SQLiteParameter("width", form.Size.Width));
                cmd.Parameters.Add(new SQLiteParameter("height", form.Size.Height));
                cmd.Parameters.Add(new SQLiteParameter("text", form.Text));
                cmd.Parameters.Add(new SQLiteParameter("formtype", form.GetType().ToString()));
                cmd.Parameters.Add(new SQLiteParameter("visible", form.Visible.ToString()));


                cmd.ExecuteNonQuery();
                rdbo.AddRelations(form);

                //添加直线
                SaveLines(form);
               

                //if (form.diagramList != null)
                //{
                //    foreach (OriginalDiagram od in form.diagramList)
                //    {
                //        SQLiteCommand cmd1 = this.GetCmd();
                //        cmd1.CommandText = "insert into  r_form_diagram(id,formid,diagramid) values (@id,@formid,@diagramid)";
                //        cmd1.Parameters.Add(new SQLiteParameter("id", System.Guid.NewGuid()));
                //        cmd1.Parameters.Add(new SQLiteParameter("formid", form.id));
                //        cmd1.Parameters.Add(new SQLiteParameter("diagramid", od.Id));

                //        cmd.ExecuteNonQuery();
                //    }
                //}
                
                this.CloseConnection();
                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }



        }

        public DummyModelForm GetFormById(String id, MainForm mainForm)
        {
            try
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "select * from OriginalModelForm where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", id));
                SQLiteDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    DummyModelForm form;
                    if ("DockSample.ObjectModelForm".Equals(/*--this.GetSimpleType(*/reader["formtype"].ToString()))//)//DockSample.Ob
                    {
                  //      Console.WriteLine("1111111111");
                        form = new ObjectModelForm();//mainForm.createNewObjectModelForm("");//

                        form.TopLevel = false;
                        form.Parent = mainForm;
                        //mainForm.splitContainer3.Panel2.Controls.Add(form);


                        form.id = id;
                        //--Point p = new Point(int.Parse(reader["x"].ToString()), int.Parse(reader["y"].ToString()));
                        //--form.Location = p;
                        //--Size size = new Size(int.Parse(reader["width"].ToString()), int.Parse(reader["height"].ToString()));
                        form.Text = reader["text"].ToString();
                        form.Visible = bool.Parse(reader["visible"].ToString());
                      //  Console.WriteLine("form.Visible is"+form.Visible);


                        form.diagramList = rdbo.GetRelations(form);

                        //得到直线列表
                        form.straightlineList = lineDBO.GetLineListByParentForm(form);

                        return form;
                    }
                    else if ("DockSample.StateTransitionModelForm".Equals(reader["formtype"].ToString()))
                    {
                        form = new StateTransitionModelForm();

                        form.TopLevel = false;
                        form.Parent = mainForm;
                        //mainForm.splitContainer3.Panel2.Controls.Add(form);


                        form.id = id;
                        //Point p = new Point(int.Parse(reader["x"].ToString()), int.Parse(reader["y"].ToString()));
                        //form.Location = p;
                        //Size size = new Size(int.Parse(reader["width"].ToString()), int.Parse(reader["height"].ToString()));
                        form.Text = reader["text"].ToString();
                        form.Visible = bool.Parse(reader["visible"].ToString());
                     //   Console.WriteLine("form.Visible is" + form.Visible);


                        form.diagramList = rdbo.GetRelations(form);
                        //得到直线列表
                        form.straightlineList = lineDBO.GetLineListByParentForm(form);

                        return form;
                    }
                    else if ("DockSample.InformationFlowModelForm".Equals(reader["formtype"].ToString()))
                    {
                        form = new InformationFlowModelForm();


                        form.TopLevel = false;
                        form.Parent = mainForm;
                        //mainForm.splitContainer3.Panel2.Controls.Add(form);

         
                        form.id = id;
                        //Point p = new Point(int.Parse(reader["x"].ToString()), int.Parse(reader["y"].ToString()));
                        //form.Location = p;
                        //Size size = new Size(int.Parse(reader["width"].ToString()), int.Parse(reader["height"].ToString()));
                        form.Text = reader["text"].ToString();
                        form.Visible = bool.Parse(reader["visible"].ToString());
                   //     Console.WriteLine("form.Visible is" + form.Visible);


                        form.diagramList = rdbo.GetRelations(form);
                        //得到直线列表
                        form.straightlineList = lineDBO.GetLineListByParentForm(form);

                        return form;
                    }
                    else 
                    {
                        return null;
                    }
                    
                   

                }
                else
                {
                    return null;
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }


        }



        private void SaveLines(DummyModelForm form)
        {
            if (form.straightlineList != null)
            {
                foreach (OriginalLine line in form.straightlineList)
                {
                    if (line is ComplexLine)
                    {
                        ComplexLineDAO cDAO = new ComplexLineDAO();
                        cDAO.SaveComlpexLine((ComplexLine)line, form.id);
                    }
                    else
                    {
                        lineDBO.AddLine(line, form.id);
                    }

                    
             //       Console.WriteLine("add a line");
                }
            } 
        }






















//应该是不用的方法
//         public List<OriginalDiagram> GetDiagramListByFormId(String formId)
//         {
//             List<OriginalDiagram> list = new List<OriginalDiagram>();
//             SQLiteCommand cmd = GetCmd();
//             cmd.CommandText = "select * from r_form_diagram where formid=@formid";
//             cmd.Parameters.Add(new SQLiteParameter("formid", formId));
//             SQLiteDataReader reader = cmd.ExecuteReader();
//             while (reader.Read())
//             {
//                 String diagramId = reader["diagramId"].ToString();
//                 String diagramType = reader["diagramType"].ToString();
//            //     String simpleDiagramType = this.GetSimpleType(diagramType);
//                 //String diagramDBOType = diagramType + "DBO";
//                 //Object diagramDBO = System.Reflection.Assembly.Load(this.projectName).CreateInstance(diagramDBOType);
//                 
//             //    list.Add(diagramDBO)
//                 //反射调用方法
//                 //不用反射了直接写
//                 if ("HighOrderObjectDiagram".Equals(this.GetSimpleType(diagramType)))
//                 {
//                 
//                     HighOrderObjectDiagramDAO dbo = new HighOrderObjectDiagramDAO();
//                     HighOrderObjectDiagram diagram= dbo.GetHOODById(diagramId);
//                     list.Add(diagram);
//                 } 
//                 else
//                 {
//                 }
//             }
//             
//             return list;
//         }
//         


    }
}
